package presenter.page;

import utils.TextUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public abstract class BasePage extends JFrame {
    private JTextArea mLoggerView;
    private final StringBuffer builder = new StringBuffer();

    protected abstract void action(String path);

    protected abstract String title();

    protected void showTip(String initTip) {
        init();
        configUI();
        log(initTip);
    }

    protected void log(String content) {
        if (!TextUtils.isEmpty(content)) {
            builder.append(content);
            builder.append("\n");
            mLoggerView.setText(builder.toString());
        }
    }

    private void init() {
        setLocation(200, 100);
        setSize(500, 300);
        setVisible(true);
        setTitle(title());
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }

    private void configUI() {
        JTextField inputView = new JTextField();
        inputView.setBackground(Color.GREEN);
        add(BorderLayout.NORTH, inputView);
        inputView.addActionListener(actionEvent -> {
            String path = inputView.getText();
            task(path);
        });
        mLoggerView = new JTextArea();
        add(BorderLayout.CENTER, new JScrollPane(mLoggerView));
    }

    private void task(String path) {
        builder.setLength(0);
        action(path);
    }
}
